package spynx.desl.parse.ui.launcher;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.Resource.Diagnostic;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

import spynx.desl.DeslProgram;

public class DeslLauncher extends LaunchConfigurationDelegate implements
		ILaunchConfigurationDelegate {

	public DeslLauncher() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void launch(ILaunchConfiguration configuration, String mode,
			ILaunch launch, IProgressMonitor monitor) throws CoreException {
		ResourceSet rs;
		rs = new ResourceSetImpl();
		Resource resource = rs.getResource(URI.createFileURI(configuration.getFile().getLocation().toString()), true);
		
		EObject eobj = resource.getContents().get(0);
		EList<Diagnostic> errors = resource.getErrors();
		
		if (errors.size() > 0) {
			for(Diagnostic err: errors) 
				System.out.printf("Error: %s", err.getMessage());
		}
		else {
			DeslProgram prog = (DeslProgram) eobj;
			prog.run(null);
		}
	}

}
